package cn.com.rocketmq.filter;

import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.client.producer.SendStatus;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.logging.org.slf4j.Logger;
import org.apache.rocketmq.logging.org.slf4j.LoggerFactory;

/**
 * 过滤生产者
 * @author xuhaixiang
 * @date 2023-09-10
 */
public class Producer {

    /**
     * 日志对象
     */
    private static  final Logger logger = LoggerFactory.getLogger(Producer.class);


    public static void main(String[] args) {

        DefaultMQProducer producer = null;
        try {
            producer = new DefaultMQProducer("syncProducer");
            producer.setNamesrvAddr("192.168.113.101:9876");
            producer.start();
            String[] tagArr = {"tagA", "tagB", "tagC"};
            for (int i = 0; i < 2; i++) {
                for (String tag : tagArr) {
                    String body = tag + ", Hello zhang " + i;
                    //参数一：主题、参数二：过滤、参数三：消息内容
                    Message message = new Message("rocketmq_syncDemo",tag, body.getBytes("UTF-8"));
                    message.putUserProperty("type", "elg_" + i);
                    SendResult result = producer.send(message);
                    String msgId = result.getMsgId();
                    SendStatus sendStatus = result.getSendStatus();
                    logger.info("{}消息发送状态为{}", msgId, sendStatus);
                }

            }
        } catch (Exception e) {
            logger.error("生产者发送消息失败！" ,e);
        }

    }
}
